#coding=utf-8
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User

from common.util import get_object_from_ct_pk


class ReportReasonManager(models.Manager):
    pass
    
class ReportReason(models.Model):
    content_type = models.ForeignKey(ContentType, verbose_name='资源类型')
    reason =  models.CharField(max_length=256, blank=True, verbose_name=u"理由")
    
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = u'举报理由'
        verbose_name_plural = u'举报理由列表'
        db_table = "t_report_reason"
        ordering = ["-created_time"]        
    
    class Admin:
        pass
        
    def __unicode__(self):
        return self.reason

class ReportManager(models.Manager):
    pass

class Report(models.Model):
    user = models.ForeignKey(User, verbose_name="用户")        
    content_type = models.ForeignKey(ContentType, verbose_name='资源类型')
    object_pk    = models.PositiveIntegerField(db_index=True, 
                                               verbose_name=u"资源id")
    content_object  = generic.GenericForeignKey('content_type', 'object_pk')
    
    report_reason =  models.ForeignKey(ReportReason, null=True)    
    is_checked = models.BooleanField(default=False, verbose_name=u'已审核', help_text=u'标识是否已审核')
    times = models.IntegerField(default=1, verbose_name=u'举报次数')
    
    created_time = models.DateTimeField(auto_now_add=True, verbose_name=u'举报时间')
    updated_time = models.DateTimeField(auto_now=True, help_text=u'更新时间')
    
    class Meta:
        verbose_name = u'举报'
        verbose_name_plural = u'举报列表'
        db_table = "t_report"
        ordering = ["-created_time"]        
    
    class Admin:
        pass
    
    
    def get_target_object(self):
        return get_object_from_ct_pk(self.content_type, self.object_pk)
    
    def __unicode__(self):
        return u"举报_%d"%(self.id)